import {messages} from "share/common";
/**
 * 操作：预约领取按钮
 * 适用：礼品申请单审核完成后申请人预约领取生成新的领用单
 */
import React from 'react'
import { connect } from 'react-redux'
import menuRoute from 'routes/menuRoute'

import { Button, message } from 'antd'

import requestService from 'containers/request/request.service'


class AppointmentBtn extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      submitLoading: false,
      applicationList: menuRoute.getRouteItem(this.props.url,'key')
    }
  }

  // 预约领取
  goAppointment = () => {
    let {giftData, receiverData, info} = this.props
    let custFormValues = info.custFormValues
    let custFormValuesJson = {}
    custFormValues && custFormValues.map(item => {
      custFormValuesJson[item.fieldCode] = item
    })
    let giftApplication = {
      applicationOID: info.applicationOID,
      baseCurrencyAmount: info.totalAmount,
      businessCode: info.businessCode,
      companyCurrencyRate: 1,
      currencyCode: info.currencyCode,
      costCenterItemCode: custFormValuesJson.costcenter ? custFormValuesJson.costcenter.valueCode : '',
      costCenterItemName: custFormValuesJson.costcenter ? custFormValuesJson.costcenter.showValue : '',
      costCenterItemOID: custFormValuesJson.costcenter ? custFormValuesJson.costcenter.value : '',
      externalParticipantNumber: 0,
      externalParticipants: [],
      participantNum: 0,
      totalBudget: info.totalAmount,
      totalSubsidiesBudget: 0,
      expenseConfirmer: '',
      recipientsNum: custFormValuesJson.number ? custFormValuesJson.number.showValue : '',
      employeeId: info.applicant.employeeID,
      isDeleted: false,
      custFormValues,
      giftReceiverDTOs: receiverData,
      giftLineDTOs: giftData
    }
    info.giftApplication = giftApplication
    this.setState({
      submitLoading: true
    }, () => {
      // requestService.appointmentCollection(info).then(res => {
      //   if (res.data.success) {
          let passData = {
            approvalTxt: "",
            conditionIds: [],
            entities: [{
              entityOID: info.applicationOID,
              entityType: 1001,
              approverOID: info.approvalChain.approverOID,
              countersignApproverOIDs: []
            }]
          }
          requestService.appointmentCollectionPass(passData).then(passRes => {
            if (passRes.data.successNum === 1) {
              message.success("预约领取成功")
              this.setState({
                submitLoading: false
              })
              // this.context.router.push(this.state.applicationList.url)
              menuRoute.goBack(this)
            } else {
              console.log("info", passRes.data.failReason[info.businessCode])
              message.error("预约领取失败，请联系tes@cn.mcd.com进行确认")
              // message.error(passRes.data.failReason[info.businessCode])
              this.setState({
                submitLoading: false
              })
            }
          }).catch(() => {
            this.setState({
              submitLoading: false
            })
          })
        // }
      // }).catch(() => {
      //   this.setState({
      //     submitLoading: false
      //   })
      // })
    })
  }

  render() {
    let { submitLoading } = this.state
    return(
      <div className="go-back-btn request-btn">
        <Button type="primary" onClick={this.goAppointment} loading={submitLoading}>{messages("common.appointment")/*预约领取*/}</Button>
      </div>
    )
  }
}

AppointmentBtn.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    language: state.main.language,
    newProfile: state.login.options,
  }
}

export default connect(mapStateToProps)(AppointmentBtn)